<?php

class add_edit_product extends a_admin_controller
{
    protected $categoryTree;
    protected $product;
    //protected $products;
    protected $product_extra_fields;
    protected $action;
    protected $cat_path;
    protected $nncis;

    public function __construct() {
        parent::__construct();
        $objCatService = new service_l_category();
        $this->categoryTree = $objCatService->get_cat_tree_menu("prod_tree");
        $this->action = "";

        $this->product = new product_m_product();
        $objProdService = new service_l_product();
        $this->product_extra_fields = $objProdService->get_extra_field_data($this->product->id);
        $this->products = array();
        $this->cat_path = "";

        $this->nncis = new nnci_m_product();
        $this->nncis->get();
        


//        $prod = $objProdService->get_product(12);
//        //echo "Data: ".$obj;
//       $taxes = $prod->get_taxes();
//       foreach($taxes as $image)
//            echo "TAX: ".$image->tax_id;




       
        
        
        //$products = array();
        //$objCatService = new service_l_category();
        //$products = $objCatService->get_products(4);
        //echo $products->count();
        //var_dump($products);
       // echo $products->get_product_obj()->part_num;
        //echo $products->get_product_obj()->get_description()->short_desc;
    }

    public function batch_assign(){
       $this->assign("tree", $this->categoryTree);
       $this->assign("product", $this->product);
       $this->assign("product_extra_fields", $this->product_extra_fields);
       $this->assign("products", $this->products);
       $this->assign("action", $this->action);
       $this->assign("cat_path", $this->cat_path);
       $this->assign("nncis",$this->nncis);
    }
    
    public function index()
    {

    }

    public function edit_product($prod_id,  $cat_id)
    {
        $objCatService = new service_l_category();
        $this->products = $objCatService->get_products($cat_id);
        
        $objProdService = new service_l_product();
        $this->product = $objProdService->get_product($prod_id);
        $this->product_extra_fields = $objProdService->get_extra_field_data($this->product->id);
        
        $this->action = "edit";
    }

    public function view_product($prod_id, $cat_id)
    {
        $objCatService = new service_l_category();
        $this->products = $objCatService->get_products($cat_id);

        $objProdService = new service_l_product();
        $this->product = $objProdService->get_product($prod_id);
        $this->product_extra_fields = $objProdService->get_extra_field_data($this->product->id);

        $this->action = "view";
    }

    public function delete_product($prod_id)
    {

    }

    public function save_product()
    {
        $deleted_prod_ids = $this->input->post('chkProdImagesDeleted', TRUE);
        //for($i = 0; $i<count($deleted_prod_ids);$i++)
         //   echo "IDs: ".$deleted_prod_ids[$i];
        //if (is_string(23)) 

        
        $vars = $this->list_post_vars();
        //if(!array_key_exists('chkHealthyChoice',$vars))
        //echo "Prod Type: ".array_key_exists('chkHealthyChoice',$vars);
        $vars['deleted_prod_ids'] = $deleted_prod_ids;
        $objProdService = new service_l_product();
        //$product = $objProdService->save_product($vars);
        
        $upload = $this->do_upload("prod_image1", $this->global_smarty_vars['product_images']."/");
        //ECHO "Image Name: ".$upload["file_name"];
    }
    public function get_products()
    {
        $requested_cat_id = $_POST['cat_id'];
        $objCatService = new service_l_category();
        $products = $objCatService->get_products($requested_cat_id);
        $requested_cat_path = $objCatService->get_cat_path($requested_cat_id, "cat_path");

         $products_json = array();
         $products_json[] = array("Cat_path" => $requested_cat_path);
         //$products_json[] = array("ID" => "Successful: ");
         foreach($products as $product)
         {
             $products_json[] = array("ID" => $product->get_product_obj()->id,
                                      "Name" =>$product->get_product_obj()->get_description()->short_desc,
                                      "Price" =>$product->get_product_obj()->price,
                                      "Type" =>"Perishable",
                                      "Tax" =>"GST & PST",
                                      "Cost" =>"1.02 $",
                                      "UPC" =>$product->get_product_obj()->upc,
                                      "Action" => "<img src='".$this->global_smarty_vars['core_images']."/common/detail.png' title='Details' style='cursor:pointer'".
                                                        "onclick=\"document.location = '".$this->global_smarty_vars['base_index_url']."/product/admin/add_edit_product/view_product/"
                                                                   .$product->get_product_obj()->id."/".$requested_cat_id."'\"> &nbsp;&nbsp;&nbsp;&nbsp".
                                                   "<img src='".$this->global_smarty_vars['core_images']."/common/edit.png' title='Edit/Update' style='cursor:pointer'".
                                                        "onclick=\"document.location = '".$this->global_smarty_vars['base_index_url']."/product/admin/add_edit_product/edit_product/"
                                                                   .$product->get_product_obj()->id."/".$requested_cat_id."'\"> &nbsp;&nbsp;&nbsp;&nbsp".
                                                  "<img src='".$this->global_smarty_vars['core_images']."/common/delete.png' title='Delete' style='cursor:pointer'".
                                                        "onclick='delete_prod(".$product->get_product_obj()->id.");'>");
         }
         echo json_encode($products_json);
    }

    protected function do_upload($field_name, $location)
    {
        $config['upload_path'] =  $location;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size']	= '100000';
        $config['max_width']  = '1024';
        $config['max_height']  = '1024';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload($field_name))
            return NULL;
        else
            return $this->upload->data();

     }

     protected function create_thumbnail($image_source)
     {
        $result = true;

        $config['image_library'] = 'gd2';
        $config['source_image'] = $image_source;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 75;
        $config['height'] = 50;

        $this->load->library('image_lib', $config);

        if ( !$this->image_lib->resize())
            $result = false;

        return $result;
     }

}
?>